define(['bootstrap', 'backend', 'table', 'form'], function (undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数
            Table.api.init({
                extend: {
                    index_url: 'scanwork/equipment_cost/index' + location.search,
                    add_url: 'scanwork/equipment_cost/add',
                    edit_url: 'scanwork/equipment_cost/edit',
                    del_url: 'scanwork/equipment_cost/del',
                    multi_url: 'scanwork/equipment_cost/multi',
                    table: 'scanwork_equipment',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'scanwork/equipment_cost/index' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('ID'), width: 50},
                        {field: 'equipment_code', title: __('设备编码'), width: 120},
                        {field: 'equipment_name', title: __('设备名称'), width: 150},
                        {field: 'equipment_type', title: __('设备类型'), width: 100},
                        {field: 'purchase_price', title: __('采购价格'), width: 100, formatter: function(v) { 
                            return '¥' + parseFloat(v || 0).toFixed(2); 
                        }},
                        {field: 'depreciation_rate', title: __('年折旧率'), width: 100, formatter: function(v) { 
                            return parseFloat(v || 0).toFixed(2) + '%'; 
                        }},
                        {field: 'hourly_cost', title: __('小时成本'), width: 100, formatter: function(v) { 
                            return '¥' + parseFloat(v || 0).toFixed(2); 
                        }},
                        {field: 'status', title: __('状态'), width: 80, formatter: function(v) { 
                            var statusMap = {0: '停用', 1: '正常'};
                            return statusMap[v] || '未知';
                        }},
                        {field: 'createtime', title: __('创建时间'), formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('操作'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});